package com.meeting.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meeting.common.request.DeleteRequest;
import com.meeting.entity.Customer;
import com.meeting.entity.MediaDeviceType;
import com.meeting.entity.MeetingRoom;
import com.meeting.entity.User;
import com.meeting.model.dto.customer.CustomerModifyRequest;
import com.meeting.model.dto.customer.CustomerQueryRequest;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeAddRequest;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeModifyRequest;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeQueryRequest;
import com.meeting.model.dto.meetingroom.MeetingRoomAddRequest;
import com.meeting.model.dto.meetingroom.MeetingRoomQueryRequest;
import com.meeting.model.dto.user.UserLoginRequest;
import com.meeting.model.dto.meetingroom.MeetingRoomModifyRequest;
import com.meeting.annotation.AuthCheck;
import com.meeting.model.dto.user.UserModifyRequest;
import com.meeting.model.dto.user.UserQueryRequest;
import com.meeting.model.enums.UserRole;
import com.meeting.common.result.Result;
import com.meeting.model.vo.CustomerVO;
import com.meeting.model.vo.UserVO;
import com.meeting.service.CustomerService;
import com.meeting.service.MediaDeviceTypeService;
import com.meeting.service.MeetingRoomService;
import com.meeting.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口")
public class AdminController {

    @Resource
    private UserService userService;

    @Resource
    private CustomerService customerService;

    @Resource
    private MeetingRoomService meetingRoomService;

    @Resource
    private MediaDeviceTypeService mediaDeviceTypeService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "管理员通过账号密码进行登录认证")
    public Result<String> login(@Valid @RequestBody UserLoginRequest userLoginRequest) {
        String token = userService.adminLogin(userLoginRequest.getUsername(), userLoginRequest.getPassword());
        return Result.success(token);
    }

    @PostMapping("add-meeting-room")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员增加会议室", description = "管理员增加会议室")
    public Result<Boolean> addMeetingRoom(@Valid @RequestBody MeetingRoomAddRequest meetingRoomAddRequest) {
        Boolean res = meetingRoomService.addMeetingRoom(meetingRoomAddRequest);
        return Result.success(res);
    }

    @PutMapping("modify-meeting-room")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员修改会议室", description = "管理员修改会议室")
    public Result<Boolean> modifyMeetingRoom(@Valid @RequestBody MeetingRoomModifyRequest meetingRoomModifyRequest) {
        Boolean res = meetingRoomService.modifyMeetingRoom(meetingRoomModifyRequest);
        return Result.success(res);
    }

    @DeleteMapping("delete-meeting-room")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员删除会议室", description = "管理员删除会议室")
    public Result<Boolean> deleteMeetingRoom(@RequestBody DeleteRequest deleteRequest) {
        Boolean res = meetingRoomService.deleteMeetingRoom(deleteRequest.getId());
        return Result.success(res);
    }

    @PostMapping("/user-list")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "获取员工列表", description = "管理员可以根据条件获取所有员工信息")
    public Result<Page<UserVO>> getEmployeeList(@RequestBody UserQueryRequest queryRequest) {
        long pageNum = queryRequest.getPageNum();
        long pageSize = queryRequest.getPageSize();
        Wrapper<User> wrapper = userService.getQueryWrapper(queryRequest);
        Page<User> userPage = userService.page(new Page<>(pageNum, pageSize), wrapper);
        
        // 创建UserVO的Page对象
        Page<UserVO> voPage = new Page<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());
        // 转换records列表
        List<UserVO> voList = userPage.getRecords().stream().map(user -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            return userVO;
        }).collect(Collectors.toList());
        voPage.setRecords(voList);
        
        return Result.success(voPage);
    }

    @PostMapping("/customer-list")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "获取客户列表", description = "管理员可以根据条件获取所有客户信息")
    public Result<Page<CustomerVO>> getCustomerList(@RequestBody CustomerQueryRequest queryRequest) {
        long pageNum = queryRequest.getPageNum();
        long pageSize = queryRequest.getPageSize();
        Wrapper<Customer> wrapper = customerService.getQueryWrapper(queryRequest);
        Page<Customer> customerPage = customerService.page(new Page<>(pageNum, pageSize), wrapper);

        // 创建CustomerVO的Page对象
        Page<CustomerVO> voPage = new Page<>(customerPage.getCurrent(), customerPage.getSize(), customerPage.getTotal());
        // 转换records列表
        List<CustomerVO> voList = customerPage.getRecords().stream().map(customer -> {
            CustomerVO customerVO = new CustomerVO();
            BeanUtils.copyProperties(customer, customerVO);
            return customerVO;
        }).collect(Collectors.toList());
        voPage.setRecords(voList);

        return Result.success(voPage);
    }

    @PutMapping("modify-user-status")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员修改用户账号状态", description = "管理员修改用户账号状态")
    public Result<Boolean> modifyUserStatus(@Valid @RequestBody UserModifyRequest userModifyRequest) {
        Boolean b = userService.modifyUserStatus(userModifyRequest);
        return Result.success(b);
    }


    @PutMapping("modify-customer-status")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员修改客户账号状态", description = "管理员修改客户账号状态")
    public Result<Boolean> modifyCustomerStatus(@Valid @RequestBody CustomerModifyRequest customerModifyRequest) {
        Boolean b = customerService.modifyCustomerStatus(customerModifyRequest);
        return Result.success(b);
    }

    @PostMapping("media-device-list")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "获取媒体设备列表", description = "管理员可以根据条件获取所有媒体设备信息")
    public Result<Page<MediaDeviceType>> getMediaDeviceTypeList(@RequestBody MediaDeviceTypeQueryRequest queryRequest) {
        long pageNum = queryRequest.getPageNum();
        long pageSize = queryRequest.getPageSize();
        Wrapper<MediaDeviceType> wrapper = mediaDeviceTypeService.getQueryWrapper(queryRequest);
        Page<MediaDeviceType> page = mediaDeviceTypeService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(page);
    }

    @PostMapping("add-media-device")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员增加媒体设备", description = "管理员增加媒体设备")
    public Result<Boolean> addMediaDeviceType(@Valid @RequestBody MediaDeviceTypeAddRequest queryRequest) {
        Boolean b = mediaDeviceTypeService.addMediaDeviceType(queryRequest);
        return Result.success(b);
    }

    @PostMapping("modify-media-device")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员修改媒体设备", description = "管理员修改媒体设备")
    public Result<Boolean> modifyMediaDeviceType(@Valid @RequestBody MediaDeviceTypeModifyRequest modifyRequest) {
        Boolean b = mediaDeviceTypeService.modifyMediaDeviceType(modifyRequest);
        return Result.success(b);
    }

    @DeleteMapping("delete-media-device")
    @AuthCheck(mustRole = UserRole.ADMIN)
    @Operation(summary = "管理员删除媒体设备", description = "管理员删除媒体设备")
    public Result<Boolean> deleteMediaDeviceType(@Valid @RequestBody DeleteRequest deleteRequest) {
        Boolean b = mediaDeviceTypeService.deleteMediaDeviceType(deleteRequest.getId());
        return Result.success(b);
    }
}
